//
//  BindingOilCardTableViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/17.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "BindingOilCardTableViewController.h"
#import "BindingPhoneTableViewCell.h"
#import "LogInPassWordFooterView.h"

@interface BindingOilCardTableViewController ()<LogInPassWordFooterDelegate,UITextFieldDelegate>

@property (nonatomic,strong)NSArray *textFieldTextArray;

@property (nonatomic,strong)NSMutableArray *textFieldMutableArray;

@end

static NSString *const reuseIdentifier = @"reuseIdentifier";

@implementation BindingOilCardTableViewController

- (NSMutableArray *)textFieldMutableArray {
    if (!_textFieldMutableArray) {
        _textFieldMutableArray = [NSMutableArray array];
    }
    return _textFieldMutableArray;
}

- (NSArray *)textFieldTextArray {
    if (!_textFieldTextArray) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"BindingOilCardText" ofType:@"plist"];
        _textFieldTextArray = [NSArray arrayWithContentsOfFile:path];
    }
    return _textFieldTextArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"绑定油卡";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = BACKGROUND_COLOR;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BindingPhoneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    if (!cell) {
        cell = [[BindingPhoneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSString *imageString = [NSString stringWithFormat:@"bangdingyouka_%ld",indexPath.section];
    cell.leftImageView.image = [UIImage imageNamed:imageString];
    cell.textField.tag = indexPath.section;
    cell.textField.placeholder = self.textFieldTextArray[indexPath.section];
    [self.textFieldMutableArray addObject:cell.textField];
    
    if (indexPath.section == 0) {
        cell.textField.hidden = YES;
        cell.rightLabel.hidden = NO;
        cell.rightLabel.text = USERNAME;
    }
    
    if (indexPath.section == 1) {
        cell.textField.secureTextEntry = YES;
        cell.textField.keyboardType = UIKeyboardTypeDefault;
        cell.textField.tag = 1;
        cell.textField.delegate = self;
    }
    
    if(indexPath.section == 2) {
        [cell.textField addTarget:self action:@selector(didClickOilCardNumber:) forControlEvents:UIControlEventEditingChanged];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return HEIGHT/5;
    } else {
        return 0;
    }
}

- (void)didClickOilCardNumber :(UITextField *)textField {
    
    UITextField *textfield = self.textFieldMutableArray.lastObject;
    
    if (textfield == textField) {
        if (textField.text.length >=19) {
            textField.text = [textField.text substringToIndex:19];
        }
        if (textField.text.length >18) {
            //            NSLog(@"%@",textField.text);
        } else {
            //            self.belongLabel.text = BelongLabelText;
        }
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 2) {
        LogInPassWordFooterView *footer = [[LogInPassWordFooterView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5)];
        footer.delegate = self;
        return footer;
    }
    return nil;
}

- (void)LogInPassWordFooterViewButton:(UIButton *)button {
    [self clearTextField];
    
    UITextField *passWordTextField = self.textFieldMutableArray[1];
    
    UITextField *oilCardTextField = self.textFieldMutableArray.lastObject;
    
    if (passWordTextField.text.length == 0 || oilCardTextField.text.length == 0) {
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请确保信息输入无误" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        [self.view addSubview:hud];
        
        return;
    }
    
    if (oilCardTextField.text.length < 16) {
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请填写正确的油卡账号" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        [self.view addSubview:hud];
        
        return;

    }
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:@{@"username":USERNAME} AndMenthodName:@"api/User/info" completeBlock:^(id object) {
        
        if (JudgeLogInData) {
            NSDictionary *dict = [object objectForKey:@"data"];
            
            NSString *oilCardString = [NSString stringWithFormat:@"%@",[dict objectForKey:@"youka"]];
            
            if (oilCardString.length) {
                
                MBProgressHUD *hud = [MBProgressHUD showMessage:@"您已绑定了油卡" toView:self.view dimBackground:NO AndCompleteBlock:nil];
                [self.view addSubview:hud];
                
            } else {
                
                NSDictionary *params = @{
                                         @"username":USERNAME,
                                         @"userpwd":passWordTextField.text,
                                         @"youka":oilCardTextField.text
                                         };
                
                [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"api/User/setUserYouka" completeBlock:^(id object) {
                    
                    NSLog(@"%@",object);
                    [JudgeBackDataCodeMessage JudgeBackDataCodeMessage:@"绑定油卡成功" andObjece:object andAddHUDView:self.view andPopViewController:^(id object) {
                        
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                }];
                
                NSLog(@"确认按钮");
            }
        }
        
    }];
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 5;
    } else {
        return 10;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    [self clearTextField];
    //    NSLog(@"%ld",self.textField.tag);
}


- (void)clearTextField {
    for (UITextField *textField in self.textFieldMutableArray) {
        [textField resignFirstResponder];
    }
}

- (void)didViewClickClearTextField {
    [self clearTextField];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self clearTextField];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 1) {
        [self.textFieldMutableArray[2] becomeFirstResponder];
    }
    
    return YES;
}
@end
